<template>
  <div class="dashboard-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>欢迎使用智慧校园系统</span>
            </div>
          </template>
          <div class="welcome-content">
            <el-avatar :size="64" :icon="UserFilled" />
            <h3>{{ userInfo.name || '学生用户' }}，您好！</h3>
            <p>今天是 {{ currentDate }}，祝您学习愉快！</p>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="mt-20">
      <el-col :span="8">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>我的课程</span>
              <el-button class="button" text>查看全部</el-button>
            </div>
          </template>
          <div class="data-overview">
            <div class="data-item">
              <div class="data-icon blue">
                <el-icon :size="24"><Reading /></el-icon>
              </div>
              <div class="data-info">
                <div class="data-title">本学期课程</div>
                <div class="data-value">{{ dashboardData.courseCount || 0 }} 门</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>我的成绩</span>
              <el-button class="button" text>查看全部</el-button>
            </div>
          </template>
          <div class="data-overview">
            <div class="data-item">
              <div class="data-icon green">
                <el-icon :size="24"><DocumentChecked /></el-icon>
              </div>
              <div class="data-info">
                <div class="data-title">已出成绩</div>
                <div class="data-value">{{ dashboardData.gradedCourseCount || 0 }} 门</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>今日课程</span>
            </div>
          </template>
          <div class="data-overview">
            <div class="data-item">
              <div class="data-icon orange">
                <el-icon :size="24"><Calendar /></el-icon>
              </div>
              <div class="data-info">
                <div class="data-title">课程数量</div>
                <div class="data-value">{{ dashboardData.todayCourseCount || 0 }} 节</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="mt-20">
      <el-col :span="24">
        <el-card v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>今日课程安排</span>
            </div>
          </template>
          <el-table :data="todayCourses" style="width: 100%">
            <el-table-column prop="time" label="时间" width="180" />
            <el-table-column prop="name" label="课程名称" />
            <el-table-column prop="teacher" label="任课教师" />
            <el-table-column prop="location" label="上课地点" />
            <el-table-column prop="status" label="状态">
              <template #default="scope">
                <el-tag :type="scope.row.status === '已结束' ? 'info' : scope.row.status === '进行中' ? 'success' : 'primary'">
                  {{ scope.row.status }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { UserFilled, Reading, DocumentChecked, Calendar } from '@element-plus/icons-vue'
import { studentApi } from '@/api'
import { ElMessage } from 'element-plus'

// 加载状态
const loading = ref(false)

// 获取当前日期
const now = new Date()
const currentDate = now.toLocaleDateString('zh-CN', { 
  year: 'numeric', 
  month: 'long', 
  day: 'numeric', 
  weekday: 'long' 
})

// 用户信息
const userInfo = ref({})

// 仪表盘数据
const dashboardData = ref({
  courseCount: 0,
  gradedCourseCount: 0,
  todayCourseCount: 0
})

// 今日课程数据
const todayCourses = ref([])

// 获取仪表盘数据
const fetchDashboardData = async () => {
  loading.value = true
  try {
    const res = await studentApi.getDashboard()
    if (res.data) {
      dashboardData.value = res.data
      todayCourses.value = res.data.todayCourses || []
    }
  } catch (error) {
    console.error('获取仪表盘数据失败:', error)
    ElMessage.error('获取仪表盘数据失败')
    // 使用模拟数据
    todayCourses.value = [
      {
        time: '08:00-09:40',
        name: '高等数学',
        teacher: '张教授',
        location: '教学楼A-101',
        status: '已结束'
      },
      {
        time: '10:00-11:40',
        name: '大学英语',
        teacher: '李教授',
        location: '教学楼B-203',
        status: '进行中'
      },
      {
        time: '14:00-15:40',
        name: '计算机基础',
        teacher: '王教授',
        location: '实验楼C-305',
        status: '未开始'
      }
    ]
  } finally {
    loading.value = false
  }
}

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const res = await studentApi.getInfo()
    if (res.data) {
      userInfo.value = res.data
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

// 页面加载时获取数据
onMounted(() => {
  fetchUserInfo()
  fetchDashboardData()
})
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 10px;
  
  .mt-20 {
    margin-top: 20px;
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .welcome-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 0;
    
    h3 {
      margin: 15px 0 5px;
      font-size: 18px;
    }
    
    p {
      color: #606266;
      margin: 5px 0;
    }
  }
  
  .data-overview {
    .data-item {
      display: flex;
      align-items: center;
      padding: 10px 0;
      
      .data-icon {
        width: 50px;
        height: 50px;
        border-radius: 8px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 15px;
        color: #fff;
        
        &.blue {
          background-color: #409EFF;
        }
        
        &.green {
          background-color: #67C23A;
        }
        
        &.orange {
          background-color: #E6A23C;
        }
      }
      
      .data-info {
        .data-title {
          font-size: 14px;
          color: #909399;
          margin-bottom: 5px;
        }
        
        .data-value {
          font-size: 20px;
          font-weight: bold;
          color: #303133;
        }
      }
    }
  }
}
</style> 